package com.yuanqunsoft.alimigration.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yuanqunsoft.alimigration.common.PropertiesUtil;
import com.yuanqunsoft.alimigration.model.AllUser;
import com.yuanqunsoft.alimigration.model.WebMenu;
import com.yuanqunsoft.alimigration.service.IAllUserService;
import com.yuanqunsoft.alimigration.utils.Filter;
import com.yuanqunsoft.alimigration.utils.FilterList;
import com.yuanqunsoft.alimigration.utils.JsonResult;

public class LoginAction extends ActionSupport {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    private String actionName;
    private boolean success;
    private String msg;
    private IAllUserService allUserService;
    private List<?> datas;
    private JsonResult result;

    public List<?> getDatas() {
        return datas;
    }

    public void setDatas(List<?> datas) {
        this.datas = datas;
    }

    public LoginAction() {
        // this.setAllUserService((IAllUserService)SpringContextUtil
        // .getBean("allUserServiceImpl"));
    }

    public IAllUserService getAllUserService() {
        return allUserService;
    }

    public void setAllUserService(IAllUserService allUserService) {
        this.allUserService = allUserService;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public JsonResult getResult() {
        return result;
    }

    public void setResult(JsonResult result) {
        this.result = result;
    }

    public String Do() {
        if (this.getActionName() == "afLoginOut")
            ActionContext.getContext().getSession().clear();
        return this.getActionName();
    }

    public String getActionName() {
        return actionName;
    }

    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public String doLogin() {
        try {
            JSONObject json = JSONObject.fromObject(datas.get(0));
            String userName = json.getString("userName");
            String userPwd = json.getString("userPwd");
            String validateCode = json.getString("validateCode");
            String sysValidCode = (String) ActionContext.getContext().getSession().get("rand");
            if (!sysValidCode.equals(validateCode)) {
                this.setSuccess(false);
                this.setMsg("验证码错误！");
            } else {
                AllUser user = allUserService.findUserByName(userName);
                if (user == null || !user.getUserPassword().equals(userPwd)) {
                    this.setSuccess(false);
                    this.setMsg("用户名或密码错误！");
                } else {
                    ActionContext.getContext().getSession().put("webUser", user);
                    ActionContext.getContext().getSession().put("userName", user.getUserName());
                    this.setSuccess(true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.setSuccess(false);
            this.setMsg("无法连接到数据库！");
        }
        return LOGIN;
    }

    public String getWebMenus() {
        AllUser user = (AllUser) ActionContext.getContext().getSession().get("webUser");
        int nodeId = Integer.parseInt(ServletActionContext.getRequest().getParameter("node"));
        short userLevel = user.getUserLevel();

        Filter<WebMenu, Integer> filter = new Filter<WebMenu, Integer>() {
            public boolean isMatched(WebMenu m, Integer mid) {
                return (m.getParentid().equals(mid) && m.getShowflag());
            }
        };

        List<WebMenu> menuList = new ArrayList<WebMenu>();

        menuList.add(new WebMenu(1, 11, 0, "系统管理", "系统管理", null, "sysManage", false, true));
        menuList.add(new WebMenu(2, 22, 1, "应用管理", "上传和管理应用", "application_Page.action", "application", true, true));
        menuList.add(new WebMenu(3, 23, 1, "报告管理", "已生成的报告列表", "report_Page.action", "report", true, true));
        menuList.add(new WebMenu(4, 24, 1, "规则集管理", "添加或修改规则集", "ruleset_Page.action", "ruleset", true, true));

        switch (userLevel) {
        case 1:
            menuList.add(new WebMenu(5, 25, 1, "规则管理", "上传和管理规则", "rule_Page.action", "rule", true, true));
            menuList.add(new WebMenu(6, 26, 1, "用户管理", "系统用户管理", "user_Page.action", "user", true, true));
            menuList.add(new WebMenu(7, 27, 1, "规则实现管理", "添加或修改规则实现", "ruleimplement_Page.action", "ruleimplement", true, true));
            break;
        default:
            break;
        }

        List<WebMenu> menus = new FilterList<Integer>().filterList(menuList, filter, nodeId);
        this.setResult(new JsonResult(menus, menus.size()));
        this.setSuccess(true);

        return SUCCESS;
    }
}
